﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ThridGroup.ERP.DTO.WarehouseManagement;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Application.Services;

namespace ThridGroup.ERP.WarehouseManagement
{
    /// <summary>
    /// 产品入库服务实现     ---tsh
    /// </summary>
    public class ProductsInboundServices : ApplicationService, IProductsInboundServices
    {
        /// <summary>
        /// 获取产品入库列表（分页查询）- 暂时使用模拟数据
        /// </summary>
        /// <param name="input">查询参数</param>
        /// <returns>分页后的产品入库列表</returns>
        public async Task<PagedResultDto<ProductsInboundDto>> GetListAsync(QueryProductsInboundDto input)
        {
            // 模拟数据
            var mockData = GetMockData();

            // 应用查询条件
            var filteredData = mockData.AsQueryable();

            if (!string.IsNullOrWhiteSpace(input.InboundNo))
            {
                filteredData = filteredData.Where(x => x.InboundNo.Contains(input.InboundNo));
            }

            if (!string.IsNullOrWhiteSpace(input.ProductCode))
            {
                filteredData = filteredData.Where(x => x.ProductCode.Contains(input.ProductCode));
            }

            if (!string.IsNullOrWhiteSpace(input.ProductName))
            {
                filteredData = filteredData.Where(x => x.ProductName.Contains(input.ProductName));
            }

            if (!string.IsNullOrWhiteSpace(input.BatchNo))
            {
                filteredData = filteredData.Where(x => x.BatchNo.Contains(input.BatchNo));
            }

            if (!string.IsNullOrWhiteSpace(input.Warehouse))
            {
                filteredData = filteredData.Where(x => x.Warehouse.Contains(input.Warehouse));
            }

            if (input.InboundDateStart.HasValue)
            {
                filteredData = filteredData.Where(x => x.InboundDate >= input.InboundDateStart.Value);
            }

            if (input.InboundDateEnd.HasValue)
            {
                filteredData = filteredData.Where(x => x.InboundDate <= input.InboundDateEnd.Value);
            }

            // 获取总数
            var totalCount = filteredData.Count();

            // 分页
            var pagedData = filteredData
                .OrderByDescending(x => x.InboundDate)
                .Skip((input.Page - 1) * input.Limit)
                .Take(input.Limit)
                .ToList();

            return await Task.FromResult(new PagedResultDto<ProductsInboundDto>
            {
                TotalCount = totalCount,
                Items = pagedData
            });
        }

        /// <summary>
        /// 根据ID获取单条产品入库信息
        /// </summary>
        /// <param name="id">产品入库ID</param>
        /// <returns>产品入库详情</returns>
        public async Task<ProductsInboundDto> GetByIdAsync(int id)
        {
            var mockData = GetMockData();
            var item = mockData.FirstOrDefault(x => x.Id == id);
            
            return await Task.FromResult(item);
        }

        /// <summary>
        /// 删除产品入库记录
        /// </summary>
        /// <param name="id">产品入库ID</param>
        /// <returns></returns>
        public async Task DeleteAsync(int id)
        {
            // 模拟删除操作
            await Task.CompletedTask;
        }

        /// <summary>
        /// 批量删除产品入库记录
        /// </summary>
        /// <param name="ids">产品入库ID列表</param>
        /// <returns></returns>
        public async Task BatchDeleteAsync(List<int> ids)
        {
            // 模拟批量删除操作
            await Task.CompletedTask;
        }

        /// <summary>
        /// 获取模拟数据
        /// </summary>
        /// <returns></returns>
        private List<ProductsInboundDto> GetMockData()
        {
            var mockData = new List<ProductsInboundDto>();
            var random = new Random();
            var warehouses = new[] { "成品仓库", "半成品仓库", "原材料仓库", "配件仓库" };
            var units = new[] { "件", "台", "套", "个", "箱" };
            var locations = new[] { "A-01-01", "A-01-02", "B-01-01", "B-01-02", "C-01-01" };

            for (int i = 1; i <= 50; i++)
            {
                mockData.Add(new ProductsInboundDto
                {
                    Id = i,
                    InboundNo = $"RK{DateTime.Now.Year}{i:D4}",
                    ProductCode = $"P{i:D4}",
                    ProductName = $"产品{i}",
                    Specification = $"规格型号{i}",
                    InboundQuantity = random.Next(10, 500),
                    Unit = units[random.Next(units.Length)],
                    BatchNo = $"PC{DateTime.Now:yyyyMM}{i:D3}",
                    InboundDate = DateTime.Now.AddDays(-random.Next(0, 30)),
                    Warehouse = warehouses[random.Next(warehouses.Length)],
                    Location = locations[random.Next(locations.Length)],
                    Operator = $"操作员{random.Next(1, 6)}",
                    Remark = i % 3 == 0 ? $"备注信息{i}" : "",
                    CreationTime = DateTime.Now.AddDays(-random.Next(0, 30))
                });
            }

            return mockData;
        }
    }
}
